package com.hao.tool.web.module.computer.controller;

import com.hao.tool.common.entity.LayResponse;
import com.hao.tool.web.common.constant.Commons;
import com.hao.tool.web.module.computer.dto.ApplicationDTO;
import com.hao.tool.web.module.computer.entity.ApplicationInfo;
import com.hao.tool.web.module.computer.entity.ComputerInfo;
import com.hao.tool.web.module.computer.service.ApplicationService;
import com.hao.tool.web.module.computer.service.ComputerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-21 21:47
 */
@RestController
@RequestMapping("/rest/application")
public class ApplicationInfoRestController {
    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private ComputerService computerService;

    @GetMapping("/list")
    public LayResponse lis(HttpServletRequest request) {
        String userName = (String) request.getAttribute(Commons.USER_NAME);
        List<ApplicationInfo> info = applicationService.getByUser(userName);
        if (info != null) {
            return LayResponse.builder().build().setCode(LayResponse.SUCCESS)
                    .setData(info);
        } else {
            return LayResponse.builder().build().setCode(LayResponse.ERROR);
        }
    }


    @PostMapping("/add")
    public LayResponse add(ApplicationDTO dto, String userName, HttpServletRequest request) {
        userName = (String) request.getAttribute(Commons.USER_NAME);

        ApplicationInfo info = applicationService.save(dto, userName);
        return LayResponse.builder().build().setCode(LayResponse.ERROR)
                .setData(info);
    }

    @GetMapping("/get")
    public LayResponse get(Integer id) {
        ApplicationInfo info = applicationService.getById(id);
        if (info == null) {
            return LayResponse.builder().build().setCode(LayResponse.ERROR);
        }
        ComputerInfo computerInfo = computerService.getById(info.getComId());
        if (computerInfo == null) {
            return LayResponse.builder().build().setCode(LayResponse.ERROR);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("application", info);
        map.put("computer", computerInfo);
        return LayResponse.builder().build().setCode(LayResponse.SUCCESS)
                .setData(map);
    }

    @GetMapping("/sh/{id}")
    public String sh(@PathVariable Integer id) {
        ApplicationInfo info = applicationService.getById(id);
        if (info == null) {
            return null;
        }
        ComputerInfo computerInfo = computerService.getById(info.getComId());
        if (computerInfo == null) {
            return null;
        }
        List<String> list = new ArrayList<>();
        list.add(computerInfo.getIp());
        list.add(computerInfo.getUsername());
        list.add(computerInfo.getPassword());
        list.add(info.getLocation());
        list.add(computerInfo.getComName());
        list.add(info.getAppName());
        return StringUtils.join(list, " ");

    }
}
